{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ed266210",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello from lambda expression\n",
      "Hello from lambda expression with int: 5 and string: test\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "// 无参数,无返回值\n",
    "Action myAction = () =>\n",
    "{\n",
    "    Console.WriteLine(\"Hello from lambda expression\");\n",
    "};\n",
    "myAction.Invoke();\n",
    "\n",
    "//有参数\n",
    "Action<int, string> myAction2 = (x, y) =>\n",
    "{\n",
    "    Console.WriteLine($\"Hello from lambda expression with int: {x} and string: {y}\");\n",
    "};\n",
    "myAction2.Invoke(5, \"test\");\n",
    "\n",
    "// 有返回值\n",
    "Func<int, string> myFunc = (x) =>\n",
    "{\n",
    "    return (x * 2).ToString();\n",
    "};\n",
    "Console.WriteLine(myFunc.Invoke(5));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "513d10d5",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "Action 10 invoked\n",
      "Closure variable: 0\n",
      "Action 10 invoked\n",
      "Closure variable: 1\n",
      "Action 10 invoked\n",
      "Closure variable: 2\n",
      "Action 10 invoked\n",
      "Closure variable: 3\n",
      "Action 10 invoked\n",
      "Closure variable: 4\n",
      "Action 10 invoked\n",
      "Closure variable: 5\n",
      "Action 10 invoked\n",
      "Closure variable: 6\n",
      "Action 10 invoked\n",
      "Closure variable: 7\n",
      "Action 10 invoked\n",
      "Closure variable: 8\n",
      "Action 10 invoked\n",
      "Closure variable: 9\n"
     ]
    }
   ],
   "source": [
    "// 闭包:\n",
    "// 内层的函数可以引用包含在它外层的函数的变量,即使外层函数的执行已经终止\n",
    "// 注意:该变量提供的值并非变量创建时的值,而是在父函数范围内的最终值\n",
    "\n",
    "class ClosureExample\n",
    "{\n",
    "    public event Action action=null;\n",
    "\n",
    "    public ClosureExample() {\n",
    "        int value = 0;\n",
    "        this.action += () => {\n",
    "            value++;\n",
    "            Console.WriteLine(value); // 内层的函数可以引用包含在它外层的函数的变量,即使外层函数的执行已经终止\n",
    "            // value的生命周期被延长\n",
    "        };\n",
    "\n",
    "        //注意:该变量提供的值并非变量创建时的值,而是在父函数范围内的最终值,在使用i时循环结束,i变为10。i的生命周期被延迟\n",
    "        for(int i = 0; i < 10; i++) {\n",
    "            int closureVariable = i; // 创建一个新的变量来捕获当前值,每次循环创建新的closureVariable\n",
    "            this.action += () => {\n",
    "                Console.WriteLine($\"Action {i} invoked\");\n",
    "                Console.WriteLine($\"Closure variable: {closureVariable}\");\n",
    "            };\n",
    "        }\n",
    "    }\n",
    "\n",
    "    public void InvokeAction()\n",
    "    {\n",
    "        this.action?.Invoke();\n",
    "    }\n",
    "}\n",
    "\n",
    "ClosureExample example = new ClosureExample();\n",
    "example.InvokeAction();"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "polyglot-notebook"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
